$(document).ready(function() {
    $.get('key-gen.php',function(sResponse){
    var key = sResponse;
    var options = {
        target:        '#sendphotos',
        beforeSubmit:  showRequest,
        success:       showResponse,
	url:       'setup-save.php?set=photos&key='+key,
        type:      'post'
    };
     $("#sendphoto").ajaxForm(options);

    });

});

function showRequest(formData, jqForm, options) {
    var queryString = $.param(formData);
    return true;
}

function showResponse(responseText, statusText)  {
$(this).hide();
$("#sendphotos").slideUp("slow");
window.location.replace('edit-photos.php');
}

function pagine(n) {
  var n = n;
  var action = "editPhoto";
  var url = 'list_all_photos.php';
  $("#interphoto").append('<img src="images/loadinfo.gif" alt="Loading" id="loading" />');
  $.get(url,
   				{ s: n,action:action  },function(sResponse){
				$('#interphoto').fadeOut("normal", function() {
				$('#interphoto').fadeIn("normal").html(sResponse);
						});
				});
}

function editPhoto(n) {
	if ($("#editablephotos")) {
		$("#editablephotos").hide("normal");
		$("#editablephotos").remove();
	}

  var n = n;
  $("#upeditphoto").append('<div id="editablephotos"></div>');
      $.get('key-gen.php',function(sResponse){
    var key = sResponse;
    $.get("setup-save.php", {id_photo:n,key:key}, function(sResponse){
		      $("#editablephotos").show("normal");
		      $("#editablephotos").html(sResponse);
		      });
    });
}

function saveEditablePhoto() {
$(".error").remove();
var url = 'list_all_photos.php';
  var Ftitle = $('#title').val();
  var Fcat = $('#kategoria').val();
  var desc = $('#desc').val();
  var id = $('#id').val();
  if ( $('#public').attr('checked')) { var pub = 1; } else { var pub = 0; }
  if ( $('#delete').attr('checked')) { var del = 1; } else { var del = 0; }
  hasError = false;
    if ( del == 0 ){
    if ( Ftitle < 1 ) {
	  hasError = true;
	  $("#title").after('<span class="error">Podaj tytuł.</span>');
	}
	if ( Fcat == 0 ) {
	  hasError = true;
	  $("#kategoria").after('<span class="error">Wybierz kategorię.</span>');
	}
	}
	if(hasError == false) {
		var dane = $("#photoedit").serialize();
	    $.get('key-gen.php',function(sResponse){
	var key = sResponse;
	  $.post("setup-save.php?set=editablephoto&key="+key,
	    dane,function(sResponse){
		    $('#editablephotos').slideUp("normal");
		    $('#editablephotos').slideDown("normal").html(sResponse);
		    pagine(1);

					      });
	});
	}
  return false;
}
